import React from 'react';
import {injectIntl} from './intl';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {
    PAGE,
    TABLE,
    PAGINATION_OPTIONS,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
    DATE_FORMAT,
    DATETIME_FORMAT,
} from '../constants';
import {Link} from 'react-router-dom';
import {conventSorter} from './utils';
import {currency, invoiceStatusHis, shipmentTypeHis} from '../Enum';
import {conventEnumValueToString, formatAmount, formatDateTime} from '../utils';
import styles from './style.css';
export class HistoryTablePanel extends React.PureComponent {
    state = {
        loading: false
    };

    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };
    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.dealer',
                    defaultMessage: '总代'
                }),
                dataIndex: 'dealerCode',
                sorter: true,
                render: (text, record) => `(${record.dealerCode})${record.dealerName}`
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.code',
                    defaultMessage: '发票号'
                }),
                sorter: true,
                dataIndex: 'code',
                render: (text, record) => (
                    <Link key="historyDetail" to={routes.historyDetail.format(record.id)}>
                        {text}
                    </Link>
                )
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.partIvoiceNumber',
                    defaultMessage: '发票数量	'
                }),
                dataIndex: 'partIvoiceNumber',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.currency',
                    defaultMessage: '币种'
                }),
                dataIndex: 'currency',
                render: text => conventEnumValueToString(currency, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.invoiceAmount',
                    defaultMessage: '发票金额'
                }),
                dataIndex: 'invoiceAmount',
                render: text => formatAmount(text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.freight',
                    defaultMessage: '运费'
                }),
                dataIndex: 'freight',
                render: text => formatAmount(text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.insurance',
                    defaultMessage: '保费'
                }),
                dataIndex: 'insurance',
                render: text => formatAmount(text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.invoicedDate',
                    defaultMessage: '发票时间'
                }),
                dataIndex: 'invoicedDate',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.status',
                    defaultMessage: '发票状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(invoiceStatusHis, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.orderCodes',
                    defaultMessage: '订单编号'
                }),
                dataIndex: 'orderCodes',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.preReceiptDate',
                    defaultMessage: '预计到港日期'
                }),
                dataIndex: 'preReceiptDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.dataSendDate',
                    defaultMessage: '出运资料发送时间'
                }),
                dataIndex: 'dataSendDate',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.shipmentType',
                    defaultMessage: '发运方式'
                }),
                dataIndex: 'shipmentType',
                render: text => conventEnumValueToString(shipmentTypeHis, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'historyTablePanel.column.toPortDate',
                    defaultMessage: '实际到港日期'
                }),
                dataIndex: 'toPortDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            }
        ];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </div>
        );
    }
}
HistoryTablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableHistorySearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'historyList', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'historyList', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'historyList', 'message']),
    total: state.getIn(['page', 'domainData', 'historyList', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'historyQueryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'historyQueryBySort', 'pageSize']),
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableHistorySearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableHistorySearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableHistorySearch(option)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(HistoryTablePanel));
